package com.lx.util;

import com.lx.entity.Cinema;
import com.lx.entity.Film;
import com.lx.entity.Studio;

import java.time.LocalDate;
import java.util.*;

/**
 * @author c先生
 * @date 2022年03月31日 下午 7:45:57
 * @describe
 */

public class FilmManager {
    HashMap<Cinema, List<Film>> map = new HashMap();
    //影片的编号
    Integer filmId = 1006;

    //根据电影类型查询影片
    public Set<Film> findFilmByType(String filmType) {
        Set<Film> list = new HashSet<>();
        String[] filmTypes = filmType.split(",");
        String filmType1 = null;
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            List<Film> films = map.get(cinema);
            for (Film film : films) {
                filmType1 = film.getFilmType();
                String[] filmTypes1 = filmType1.split(",");
                for (int i = 0; i < filmTypes1.length; i++) {
                    for (int j = 0; j < filmTypes.length; j++) {
                        if (filmTypes[j].equals(filmTypes1[i])) {
                            list.add(film);
                        }
                    }
                }
            }
        }
            return list;
    }
    //下架影片
    public boolean removeFilm(String filmName,String cinemaNameStr){
        boolean flag = false;
        if("all".equals(cinemaNameStr)){
            Set<Cinema> cinemas = map.keySet();
            for (Cinema cinema : cinemas) {
                if(existFilm(filmName,cinema)!=null){
                    map.get(cinema).remove(existFilm(filmName));
                    flag = true;
                }

            }
        }else {
            String[] strings = cinemaNameStr.split(",");
            if(strings.length==1){
                Set<Cinema> cinemas = map.keySet();
                for (Cinema cinema : cinemas) {
                    if(cinema.getCinemaName().equals(strings[0])){
                        if(existFilm(filmName,cinema)!=null) {
                            map.get(cinema).remove(existFilm(filmName));
                            flag = true;
                        }
                    }
                }
            }else {
                for (int i = 0; i < strings.length; i++) {
                    Set<Cinema> cinemas = map.keySet();
                    for (Cinema cinema : cinemas) {
                        if(cinema.getCinemaName().equals(strings[i])){
                            if(existFilm(filmName,cinema)!=null) {
                                map.get(cinema).remove(existFilm(filmName));
                                flag = true;
                            }

                        }
                    }
                }
            }
        }
        return flag;
    }
    //根据影院的名称 上映新影片
    public boolean addFilmAndPublication(Film addFilm,String cinemaNameStr){
        addFilm.setFilmId(filmId);
        filmId++;
        if("all".equals(cinemaNameStr)){
            Set<Cinema> cinemas = map.keySet();
            for (Cinema cinema : cinemas) {
                map.get(cinema).add(addFilm);
                return true;
            }
        }else {
            String[] strings = cinemaNameStr.split(",");
            if(strings.length==1){
                Set<Cinema> cinemas = map.keySet();
                for (Cinema cinema : cinemas) {
                    if(cinema.getCinemaName().equals(strings[0])){
                        map.get(cinema).add(addFilm);
                        return true;
                    }
                }
            }else{
                for (int i = 0; i < strings.length; i++) {
                    Set<Cinema> cinemas = map.keySet();
                    for (Cinema cinema : cinemas) {
                        if(cinema.getCinemaName().equals(strings[i])){
                            map.get(cinema).add(addFilm);
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    //判断影片是否存在
    public Film existFilm(String FilmName){
        HashSet<Film> films = showAllFilm();
        for (Film film : films) {
            if(film.getFilmName().equals(FilmName)){
                return film;
            }
        }
        return null;
    }
    //判断影片是否存在
    public Film existFilm(String FilmName,Cinema cinema){
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema1 : cinemas) {
            if(cinema1.getCinemaName().equals(cinema.getCinemaName())) {
                List<Film> films1 = map.get(cinema1);
                for (Film film : films1) {
                    if (film.getFilmName().equals(FilmName)) {
                        return film;
                    }
                }
            }
        }

    return null;
    }
    //根据影院的名称查看旗下放映的影片
    public void showAllFilmByCinemaName(String cinemaName){
        Cinema cinema = getCinemaByName(cinemaName);
        if(cinema != null){
            if(cinema.getCinemaName().equals(cinemaName)){
                for (Film film : map.get(cinema)) {
                    System.out.println(film.toString());
                }
            }
        }else {
            System.out.println("此影院不存在！");
        }
    }

    //根据影院的名字获取影院对象
    public Cinema getCinemaByName(String cinemaName) {
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            if (cinemaName.equals(cinema.getCinemaName())) {
                return cinema;
            }
        }
        return null;
    }
    //展示所有影院名称
    public List<String> showAllCinemaName(){
        Set<Cinema> cinemas = map.keySet();
        List<String> cinemaNameList = new ArrayList<>();
        for (Cinema cinema : cinemas) {
            cinemaNameList.add(cinema.getCinemaName());
        }
        return cinemaNameList;
    }

    //展示所有的影片信息
    public HashSet<Film> showAllFilm(){
        Set<Cinema> cinemas = map.keySet();
        HashSet<Film> set = new HashSet<>();
        for (Cinema cinema : cinemas) {
            List<Film> films = map.get(cinema);
            for (Film film  : films) {
                set.add(film);
            }
        }
        return set;
    }

    //展示所有影院的信息
    public void showAllCinema(){
        Set<Cinema> cinemas = map.keySet();
        for (Cinema cinema : cinemas) {
            System.out.println(cinema.toString());
        }
    }

    //初始化数据
    public void init() {
        LocalDate filmTime1 = LocalDate.of(2018, 1, 2);
        Film film1 = new Film(1001, "蝴蝶效应", "惊悚,科幻", "90分钟", "林立,冰冰", "这是一个穿越时间的电影", filmTime1);
        LocalDate filmTime2 = LocalDate.of(2020, 1, 1);
        Film film2 = new Film(1002, "长津湖", "战争,热血", "180分钟", "吴京,易烊千玺", "这是一个战争的影片", filmTime2);
        LocalDate filmTime3 = LocalDate.of(2019, 1, 1);
        Film film3 = new Film(1003, "流浪地球", "灾难,科幻", "170分钟", "吴京,吴孟达", "这是一个地球没了逃跑的电影", filmTime3);
        ArrayList<Film> films = new ArrayList<>();
        films.add(film1);
        films.add(film2);
        films.add(film3);
        Studio studio1 = new Studio(2001, "花开富贵厅", 50);
        Studio studio2 = new Studio(2002, "粉色玫瑰厅", 45);
        Studio studio3 = new Studio(2003, "荷塘月色厅", 7);
        ArrayList<Studio> studios = new ArrayList<>();
        studios.add(studio1);
        studios.add(studio2);
        studios.add(studio3);
        Cinema cinema = new Cinema(3001, "夕阳红电影院", "村头一号院", studios);
        map.put(cinema, films);

        LocalDate filmTime4 = LocalDate.of(2012, 8, 2);
        Film film4 = new Film(1004, "迪迦奥特曼大电影", "抗日,科幻", "120分钟", "大古,丽娜", "这是一个抗日的大电影", filmTime4);
        LocalDate filmTime5 = LocalDate.of(2019, 1, 5);
        Film film5 = new Film(1005, "大话西游", "爱情,抗日", "140分钟", "吴孟达,周星驰", "这是一个恶搞的电影", filmTime5);
        ArrayList<Film> films2 = new ArrayList<>();
        films2.add(film4);
        films2.add(film5);
        films2.add(film3);
        Studio studio4 = new Studio(2004, "VIP厅", 30);
        Studio studio5 = new Studio(2005, "SVIP厅", 40);
        ArrayList<Studio> studios2 = new ArrayList<>();
        studios2.add(studio4);
        studios2.add(studio5);
        Cinema cinema2 = new Cinema(3002, "万达影院", "万达广场", studios2);
        map.put(cinema2, films2);
    }
}
